package com.filefilter.asyn;

import com.filefilter.util.FileMonitorUtils;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;

/**
 * \* Created with IntelliJ IDEA.
 * \* Author: fly
 * \* Date: 2020/6/14
 * \* Time: 22:19
 * \* Description:
 * \
 */
@Component
public class AsynTask {

    @Resource
    private FileMonitorUtils fileUtils;

    /**
     * 线程执行 ： 以防止多文件进入堵塞
     */
    @Async("taskExecutor")
    public void taskOne(){
        System.out.println(Thread.currentThread().getName());
        //  监控的文件夹路径
        File directory = new File("D:\\FileFilterDemo\\img");
        if (!directory.exists()){
            throw new NullPointerException("目录不存在启动文件观察者失败" + directory);
        }
        //  监控间隔
        Long intervalSeconds = 5L;
        //   监控指定前缀
        String prefix = "test_";
        //   监控指定后缀文件
        String suffix = ".txt";
        try{
            FileAlterationMonitor monitor = fileUtils.getMonitor(directory,intervalSeconds,
                    prefix,suffix);
            monitor.start();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}

